#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "serversocket.h"
#include "socket.h"
#include "factory.h"
#include "epollevent.h"
#include "epoll.h"
#define MAXBUF 1024

int main(int argc, char **argv)
{
	rlimit rt;
	rt.rlim_max = rt.rlim_cur = 1024;
	if (setrlimit(RLIMIT_NOFILE, &rt) == -1)
	{
		perror("setrlimit");
		exit(1);
	}

	ServerSocket serverSocket(8001);
	serverSocket.Listen();
	Epoll* epoll = Epoll::EpollCreate();
	EpollEvent* epollEvent = Factory::CreatListenEpollEvent(&serverSocket);
	epoll->Epoll_ctl(EPOLL_CTL_ADD, *epollEvent);
	epoll_event events[MAXBUF];
	int count = 0;
	while (1)
	{
		printf("the %d waiting\n",count);
		count = count + 1;
		int	nfds = epoll->Epoll_wait(events,MAXBUF);
		if (nfds == -1)
		{
			perror("epoll_wait");
			break;
		}
		for(int i=0; i<nfds; i++){
			EpollEvent* e = Factory::CreatEpollEvent(events[i]);
			e->DoEvent();
			delete e;
		}
		printf("end wait\n");
	}
	return 0;
}

